package com.gshrms.org.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gshrms.common.exception.org.OrgParentNotExistsException;
import com.gshrms.common.exception.org.OrgPostSerialParentNotExistsException;
import com.gshrms.common.utils.DateUtils;
import com.gshrms.org.domain.OrgInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gshrms.org.mapper.OrgPostSerialMapper;
import com.gshrms.org.domain.OrgPostSerial;
import com.gshrms.org.service.IOrgPostSerialService;

/**
 * 职位序列Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class OrgPostSerialServiceImpl extends ServiceImpl<OrgPostSerialMapper,OrgPostSerial> implements IOrgPostSerialService
{
    @Autowired
    private OrgPostSerialMapper orgPostSerialMapper;

    /**
     * 查询职位序列
     * 
     * @param postSerialId 职位序列主键
     * @return 职位序列
     */
    @Override
    public OrgPostSerial selectOrgPostSerialByPostSerialId(Long postSerialId)
    {
        return orgPostSerialMapper.selectOrgPostSerialByPostSerialId(postSerialId);
    }

    /**
     * 查询职位序列列表
     * 
     * @param orgPostSerial 职位序列
     * @return 职位序列
     */
    @Override
    public List<OrgPostSerial> selectOrgPostSerialList(OrgPostSerial orgPostSerial)
    {
        return orgPostSerialMapper.selectOrgPostSerialList(orgPostSerial);
    }

    /**
     * 查询是否存在子节点
     *
     * @param postSerialId 职位序列主键
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean hasChildByPostSerialId(Long postSerialId){
        int result = orgPostSerialMapper.hasChildByPostSerialId(postSerialId);
        return result > 0;
    }


    /**
     * 新增职位序列
     * 
     * @param orgPostSerial 职位序列
     * @return 结果
     */
    @Override
    public int insertOrgPostSerial(OrgPostSerial orgPostSerial)
    {
        // 获取父组织信息
        if(orgPostSerial.getParentId().equals(0L)){
            orgPostSerial.setAncestors("0");
        }else{
            OrgPostSerial orgPostSerialParent = orgPostSerialMapper.selectOrgPostSerialByPostSerialId(orgPostSerial.getParentId());
            if(orgPostSerialParent == null){
                throw new OrgPostSerialParentNotExistsException();
            }

            orgPostSerial.setAncestors(orgPostSerialParent.getAncestors() + "," + orgPostSerialParent.getPostSerialId());
        }

        orgPostSerial.setCreateTime(DateUtils.getNowDate());
        return orgPostSerialMapper.insertOrgPostSerial(orgPostSerial);
    }

    /**
     * 修改职位序列
     * 
     * @param orgPostSerial 职位序列
     * @return 结果
     */
    @Override
    public int updateOrgPostSerial(OrgPostSerial orgPostSerial)
    {
        orgPostSerial.setUpdateTime(DateUtils.getNowDate());
        return orgPostSerialMapper.updateOrgPostSerial(orgPostSerial);
    }

    /**
     * 批量删除职位序列
     * 
     * @param postSerialIds 需要删除的职位序列主键
     * @return 结果
     */
    @Override
    public int deleteOrgPostSerialByPostSerialIds(Long[] postSerialIds)
    {
        return orgPostSerialMapper.deleteOrgPostSerialByPostSerialIds(postSerialIds);
    }

    /**
     * 删除职位序列信息
     * 
     * @param postSerialId 职位序列主键
     * @return 结果
     */
    @Override
    public int deleteOrgPostSerialByPostSerialId(Long postSerialId)
    {
        return orgPostSerialMapper.deleteOrgPostSerialByPostSerialId(postSerialId);
    }
}
